package org.jxc.service;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.hibernate.SessionFactory;
import org.jxc.constants.JXCString;
import org.jxc.dao.ArriveOrderDAO;
import org.jxc.dao.ArriveOrderProductDAO;
import org.jxc.dao.PurchaseOrderDAO;
import org.jxc.dao.PurchaseOrderProductDAO;
import org.jxc.domain.ArriveOrder;
import org.jxc.domain.ArriveOrderProduct;
import org.jxc.domain.PurchaseOrder;
import org.jxc.domain.PurchaseOrderProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.team4.dao.DAOFactory;
import org.team4.exception.CRUDException;
import org.team4.service.IService;
import org.team4.util.LoginBean;
import org.team4.util.Validate;
import org.team4.vo.Page;

@Service
public class ArriveOrderService implements IService {

	private ArriveOrderDAO arriveOrderDAO;
	private ArriveOrderProductDAO arriveOrderProductDAO;
	private PurchaseOrderDAO purchaseOrderDAO;
	private PurchaseOrderProductDAO purchaseOrderProductDAO;

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		arriveOrderDAO = (ArriveOrderDAO) DAOFactory.getDAO(sessionFactory,
				ArriveOrderDAO.class);
		purchaseOrderDAO = (PurchaseOrderDAO) DAOFactory.getDAO(sessionFactory,
				PurchaseOrderDAO.class);
		purchaseOrderProductDAO = (PurchaseOrderProductDAO) DAOFactory.getDAO(
				sessionFactory, PurchaseOrderProductDAO.class);
		arriveOrderProductDAO = (ArriveOrderProductDAO) DAOFactory.getDAO(
				sessionFactory, ArriveOrderProductDAO.class);
	}

	public String save(String purchaseOrderId) throws CRUDException {
		List<ArriveOrder> aos = arriveOrderDAO.find(
				"from ArriveOrder where purchaseOrderId=?", purchaseOrderId);
		if (Validate.collectionNotNull(aos)) {
			return aos.get(0).getId();
		} else {
			PurchaseOrder po = purchaseOrderDAO.get(purchaseOrderId);
			ArriveOrder ao = new ArriveOrder();
			LoginBean bean = LoginBean.getLoginBean();
			ao.setAuditor(bean.getUser().getDepartment().getManager());
			ao.setPurchaseOrder(po);
			ao.setOrderDate(new Date());
			ao.setCreator(LoginBean.getLoginBean().getUser());
			ao.setCreateTime(new Date());
			ao.setAuditState(JXCString.NOTAPPLY);
			ao.setStoreState(JXCString.NOTINSTORE);
			ao.setDeleteFlag("0");
			arriveOrderDAO.save(ao);
			List<PurchaseOrderProduct> popList = purchaseOrderProductDAO.find(
					"from PurchaseOrderProduct where purchaseOrderId=?",
					po.getId());
			for (Iterator<PurchaseOrderProduct> iterator = popList.iterator(); iterator
					.hasNext();) {
				PurchaseOrderProduct pop = iterator.next();
				ArriveOrderProduct aop = new ArriveOrderProduct();
				aop.setAmounts(pop.getAmounts());
				aop.setInAmounts(pop.getAmounts());
				aop.setArriveOrder(ao);
				aop.setProduct(pop.getProduct());
				aop.setPrice(pop.getPrice());
				arriveOrderProductDAO.save(aop);
			}
			po.setEditor(LoginBean.getLoginBean().getUser());
			po.setEditTime(new Date());
			purchaseOrderDAO.update(po);
			return ao.getId();
		}
	}

	public ArriveOrder findArriveOrderByPurchaseOrderId(String purchaseOrderId)
			throws CRUDException {
		return (ArriveOrder) arriveOrderDAO.findUnique(
				"from ArriveOrder where purchaseOrder.id = ?", purchaseOrderId);
	}

	public void update(ArriveOrder ao) throws CRUDException {
		ArriveOrder old = arriveOrderDAO.get(ao.getId());
		old.setPurchaseOrder(ao.getPurchaseOrder());
		old.setOrderDate(ao.getOrderDate());
		old.setCode(ao.getCode());
		old.setEditor(LoginBean.getLoginBean().getUser());
		old.setEditTime(new Date());
		old.setAuditState(ao.getAuditState());
		old.setStoreState(ao.getStoreState());
		arriveOrderDAO.update(old);
	}

	public ArriveOrder findById(String id) throws CRUDException {
		return arriveOrderDAO.get(id);
	}

	public Page<ArriveOrder> listPagination(Page<ArriveOrder> page)
			throws CRUDException {
		return arriveOrderDAO.pagination(page,
				"from ArriveOrder where deleteFlag = '0'", null);
	}

	/**
	 * 批量删除到货单
	 * 
	 * @param idList
	 * @return
	 * @throws CRUDException
	 */
	public void deleteArriveOrder(String id) throws CRUDException {
		ArriveOrder ao = arriveOrderDAO.get(id);
		ao.setDeleteFlag("1");
		arriveOrderDAO.update(ao);
	}
}